#include "pch.hpp"
#include "provider.hpp"

using namespace services;




provider::provider()
{}
///////////////////////////////////////////////////////////////////////////////////////////////////

provider::~provider()
{}
///////////////////////////////////////////////////////////////////////////////////////////////////




service::ptr provider::service(QString name) const
{
	ServiceMap::const_iterator i = m_services.find(name);

	if(i == m_services.end())
		BOOSTEXT_THROW(service_missing("The given service does not exist"));

	return *i;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

bool provider::hasService(QString name) const
{
	return m_services.find(name) != m_services.end();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void provider::addService(service::ptr s)
{
	// Prevent a Service from being registered twice
	if(hasService(s->name()))
		BOOSTEXT_THROW(already_registered("The given service has already been registered"));

	// Check if all dependent services have been previously registered
	BOOST_FOREACH(QString name, s->dependencies())
		if(!hasService(name))
			BOOSTEXT_THROW(service_missing("The given service is at least missing one dependent service"));

	m_services.insert(s->name(), s);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
